package com.gs.coursera.algorithms.core.graph;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.BitSet;
import java.util.HashMap;
import java.util.Map;

public class HammingUndirectedGraph extends
		UndirectedGraph<BitSet, Short> {
	
	public static final int BIT_COUNT = 24;
	
	public HammingUndirectedGraph(BufferedReader reader) throws IOException {
		super(reader);
		String line = null;
		int lineNo = 0;
		boolean []vertexMap = new boolean[getVertexCount()];
		BitSet[] nodes = new BitSet[getVertexCount()];
		while((line = reader.readLine()) != null){
			String data = line.replaceAll(" ", "").trim();
			char[] characters = data.toCharArray();
			BitSet bitSet = new BitSet(BIT_COUNT);
			for (int i = 0; i < characters.length; i++) {
				if(characters[i] == '1')
					bitSet.set(i);
			}
			/*Vertex<BitSet, Short> v = new Vertex<BitSet, Short>(bitSet);
			v.setVertexNumber(lineNo);
			addVertex(v);*/
			nodes[lineNo] = bitSet;
			vertexMap[lineNo] = false;
			lineNo ++;
		}
		/*for(int i=0; i < getVertexCount(); i++){
			Vertex<char[], Short> vi = getVertices().get(i);
			for(int j=0; j < getVertexCount() && j != i; j++){
				Vertex<char[], Short> vj = getVertices().get(j);
				Short d = getDistance(vi, vj);
				addEdge(vi, vj, d);
			}
		}*/
		System.out.println("build edge");
		long count = 0;
		long k=0;
		
		for(int i=0; i<getVertexCount(); i++){
			for(int j=0; j < i; j++){
				if(i!=j){
					//k++;
					/*Vertex<BitSet, Short> vi = getVertices().get(i);
					Vertex<BitSet, Short> vj = getVertices().get(j);
					vi.getValue(), vj.getValue()
					*/
					Short d = getDistance(nodes[i], nodes[j]);
					//addEdge(vi, vj, d);
					if(d<=2){
						if(!vertexMap[i])
							count++;
						if(!vertexMap[j])
							count++;
						vertexMap[i] = true;
						vertexMap[j] = true;
					}
					d = 0;
				}
			}
			//System.gc();
		}
		//System.out.println(k);
		System.out.println("build edge - Done: " + count);
	}
	
	public Short getDistance(BitSet v1, BitSet v2){
		short count = 0;
		
		for (int i=0; i< BIT_COUNT; i++)
            if (v1.get(i) != v2.get(i))
                count++;
		
		return count;
	}
	

}
